/* mvtest.c
 *	Simple program to test the MV-related syscalls
 *	
 *	Does syscalls that create, set, get and destroy MVs
 */
#include "syscall.h"

void CreateMVTest();
void SetMVTest();
void GetMVTest();
void DestroyMVTest();

int main(int argc, char **argv)
{
	SetMVTest();
	DestroyMVTest();
	CreateMVTest();
}

void CreateMVTest()
{
    int mvIndex, value, i;
    PrintOut("\n\n********************CREATEMV TESTS**********************\n\n", 64, ConsoleOutput); 
    
    /*Correct Use - Should Pass*/
    PrintOut("\nTEST 1: Using Create Correctly................................\n", 64, ConsoleOutput); 
    CreateMV(1);
    
    PrintOut("\nTEST 2: Using Create Repeatedly to create different MVs.......\n", 64, ConsoleOutput); 
    for (i = 0; i < 10; i++)
    {
        CreateCV(1); /*Create a mv. An int that indexes the mvs location in an array in the kernel will be returned*/
    }

	PrintOut("\nTEST 3: Using Create to make more MVs than the maximum........\n", 64, ConsoleOutput); 
    for (i = 0; i < 800; i++)
    {
        CreateMV(1); 
    }
}

void SetMVTest()
{
    int mvIndex, i, value;
    
    PrintOut("\n\n***********************SET/GET TESTS************************\n\n", 64, ConsoleOutput); 
    
    /*Correct Use - Should Pass*/
    PrintOut("\nTEST 1: Setting an MV after creating it.......................\n", 64, ConsoleOutput);
    mvIndex = CreateMV(1);
	SetMV(mvIndex, 9); /*first int is index of MV, and second int is value to set it to*/
	value = GetMV(mvIndex);
	if(value == 9)
	{
		PrintOut("\nTEST 1: PASSED................................................\n", 64, ConsoleOutput);
	}
	else
	{
		PrintOut("\nTEST 1: FAILED................................................\n", 64, ConsoleOutput);
	}

	PrintOut("\nTEST 2: Setting an MV after it has been previously set........\n", 64, ConsoleOutput);
    SetMV(mvIndex, 566);
	value = GetMV(mvIndex);
	if(value == 566)
	{
		PrintOut("\nTEST 2: PASSED................................................\n", 64, ConsoleOutput);
	}
	else
	{
		PrintOut("\nTEST 2: FAILED................................................\n", 64, ConsoleOutput);
	}
    
    /*Incorrect Use - Should Fail and print error message*/
    PrintOut("\nTEST 3: Using Set on uninitialized int ptr....................\n", 64, ConsoleOutput);
    SetMV(500, 45); 
	
    /*Incorrect Use - Should Fail and print error message from Kernel code.*/
    PrintOut("\nTEST 4: Using Set on a mv index out of bounds.................\n", 64, ConsoleOutput);
    SetMV(900, 5); 
	
    PrintOut("\nTEST 5: Using Set on a negative lock index....................\n", 64, ConsoleOutput);
    SetMV(-2, 4);

	/*PrintOut("\nTest 6: Using Signal with numeric strings instead of ints.....\n", 64);
	SetMV("2", "2");

	PrintOut("\nTest 7: Using Signal with character strings instead of ints...\n", 64);
	SetMV("hat", "cat");*/	

	/*PrintOut("\nTest 8: Using Set with max integer value......................\n", 64);
	mvIndex = CreateMV();
	SetMV(mvIndex, INT_MAX);

	PrintOut("\nTest 9: Using Set with max integer value + 1..................\n", 64);
	mvIndex = CreateMV();
	SetMV(mvIndex, INT_MAX+1);

	PrintOut("\nTest 10: Using Set with min integer value.....................\n", 64);
	mvIndex = CreateMV();
	SetMV(mvIndex, INT_MIN);

	PrintOut("\nTest 11: Using Set with min integer value - 1.................\n", 64);
	mvIndex = CreateMV();
	SetMV(mvIndex, INT_MIN-1);*/
}



void DestroyMVTest()
{
    int mvIndex, value, i;
    
    PrintOut("\n\n********************DESTROYMV TESTS*********************\n\n", 64, ConsoleOutput); 
    
    /*Correct Use - Should Pass*/
    PrintOut("Test 1: Using Destroy after a create...........................\n", 64, ConsoleOutput); 
    mvIndex = CreateMV();
    DestroyMV(mvIndex);
    
    PrintOut("\nTest 2: Using Destroy after a Set.............................\n", 64, ConsoleOutput); 
    mvIndex = CreateMV();
    SetMV(mvIndex, 56);
    DestroyMV(mvIndex);
    
    /*Incorrect Use - Should Fail and print error message*/
    PrintOut("\nTest 3: Using Destroy on uninitialized MV.....................\n", 64, ConsoleOutput); 
    DestroyMV(50);
    
    PrintOut("\nTest 4: Using Destroy on a destroyed mv.......................\n", 64, ConsoleOutput); 
    mvIndex = CreateMV();
    DestroyMV(mvIndex);
    DestroyMV(mvIndex);
    
    PrintOut("\nTest 5: Using Destroy multiple times in a row on same CV......\n", 64, ConsoleOutput); 
    mvIndex = CreateMV();
    for(i=0; i<5; i++)
    {
        DestroyMV(mvIndex);
    }
    
    PrintOut("\nTest 6: Using Destroy at a MV index out of bounds.............\n", 64, ConsoleOutput);
    DestroyMV(600);
    
	PrintOut("\nTest 7: Using Destroy at a negative MV index..................\n", 64, ConsoleOutput);
    DestroyMV(-2);
}

